package cn.cboard.dao.sql;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import cn.cboard.dao.ForumDao;
import cn.cboard.database.Query;
import cn.cboard.model.Forum;
import cn.cboard.util.DateUtil;

import com.whirlycott.cache.Cache;
import com.whirlycott.cache.CacheException;
import com.whirlycott.cache.CacheManager;

@SuppressWarnings("unchecked")
public class ForumDaoSql implements ForumDao{
	private final static Logger logger = Logger.getLogger(ForumDaoSql.class);
	private Query query = null;	
	//whirlycott cache
	private Cache cache = null;
	public static final long TIME_OUT = DateUtil.HOUR;
		
	public ForumDaoSql(Query query){
		this.query = query;
	}

	public Collection<Forum> listForums() {
		logger.debug("[boadmx] --> List Forums");
		String sqlquery = "SELECT * FROM forumforums";
		List queryParams = new ArrayList();
		List list = new ArrayList();
		
		try {
			cache = CacheManager.getInstance().getCache("forum");
		} catch (CacheException ec) {
			logger.error(ec);
		}
		
		if(cache != null){
			list = (List) cache.retrieve("forum");		
			if(list == null){
				list = (List) query.queryMapper(queryParams,sqlquery,new ForumMapper());
				cache.store("forum", list, TIME_OUT);				
			}
		}
		else
		{
			list = (List) query.queryMapper(queryParams,sqlquery,new ForumMapper());			
		}
		query.close(); //fix bug...
		return list;
	}


	@SuppressWarnings("unchecked")
	public void create(Forum forum) {
		String sqlquery = "insert into forumforums(title,description,displayOrder) value(?,?,?)";
		List queryParams = new ArrayList();
		queryParams.add(forum.getTitle());
		queryParams.add(forum.getDescription());
		queryParams.add(forum.getDisplayOrder());
		
		query.queryExecute(queryParams, sqlquery);
	}


	public Forum getForum(Long forumId) {
		String sqlquery = "select forumid,title,description from forumforums where forumid=?";
		List queryParams = new ArrayList();
		queryParams.add(forumId);
		
		Forum ret = new Forum();
		
		List list = query.queryMultiObject(queryParams, sqlquery);
		Iterator iter = list.iterator();
		if(iter.hasNext()){
			Map map = (Map) iter.next();
			ret.setForumId((Long) map.get("forumId"));
			ret.setTitle((String) map.get("title"));
			ret.setDescription((String) map.get("description"));
		}
		
		return ret;
	}



	public void update(Forum forum) {
		String sqlquery="update forumforums set title=?, description=? where forumid=?";
		List queryParams = new ArrayList();
		queryParams.add(forum.getTitle());
		queryParams.add(forum.getDescription());
		queryParams.add(forum.getForumId());
		
		query.queryExecute(queryParams, sqlquery);
	}

	public void removeForm(Long forumId) {
		String sqlquery="delete forumforums where forumid=?";
		List queryParams = new ArrayList();
		queryParams.add(forumId);

		query.queryExecute(queryParams, sqlquery);
	}

	public Long getLastTopicId(Long forumId) {
		String sqlquery="select lastTopicId from forumforums where forumid=?";
		List queryParams = new ArrayList();
		queryParams.add(forumId);
		Long lastTopicId = new Long(0);
		lastTopicId = (Long) query.querySingleObject(queryParams, sqlquery);
		return lastTopicId;
	}

	public int getThreadCount(Long forumId) {
		String sqlquery = "select count(*) from forumthreads where forumid=?";
		List queryParams = new ArrayList();
		queryParams.add(forumId);
		
		Long threadCount = (Long) query.querySingleObject(queryParams, sqlquery);
		return threadCount.intValue();
	}

	public int getTopicCount(Long forumId) {
		String sqlquery = "select count(*) from forumtopics where forumid=?";
		List queryParams = new ArrayList();
		queryParams.add(forumId);

		Long topicCount = (Long) query.querySingleObject(queryParams, sqlquery);
		return topicCount.intValue();
	}
}
